package com.leetcode.LeetCode精选TOP面试题;

/**
 * @author 覃国强
 * @date 2022-07-01 16:16
 */
public class 题库_0152_乘积最大子数组 {

  public static void main(String[] args) {
    int[] nums = {-2, -3, -4};
    Solution solution = new Solution();
    int maxProduct = solution.maxProduct(nums);
    System.out.println(maxProduct);
  }

  static
  class Solution {
    public int maxProduct(int[] nums) {
      if (nums == null || nums.length == 0) {
        return 0;
      }
      int answer = nums[0];
      int preMax = nums[0];
      int preMin = nums[0];
      for (int i = 1; i < nums.length; ++i) {
        int curMax = Math.max(nums[i], Math.max(preMax * nums[i], preMin * nums[i]));
        int curMin = Math.min(nums[i], Math.min(preMax * nums[i], preMin * nums[i]));
        answer = Math.max(answer, curMax);
        preMax = curMax;
        preMin = curMin;
      }
      return answer;
    }
  }

}
